<?php
 require_once ROOT_PATH . '/lib/models/surveys/Questions.php';
?>
<script type="text/javascript">

function addSave() {
	
	var surveytype = <?php echo $records['survey']->getSurveyType();  ?>;
	
	if(surveytype ==2){
		var quizanswerid = document.getElementById('quizanswer').value;
		var quizanswer =  document.getElementById(quizanswerid).value;		
		if(IsNumeric(quizanswer)){
			document.frmSurveyProcess.submit();
		}else{
			alert( '"'+quizanswer+'"'+ ' is invalid. It must be a number');
			return false;
		}
	}
	
}

function IsNumeric(sText)
		{
		   var ValidChars = "0123456789";
		   var IsNumber=true;
		   var Char;		 
		   for (i = 0; i < sText.length && IsNumber == true; i++) 
		      { 
		      Char = sText.charAt(i); 
		      if (ValidChars.indexOf(Char) == -1) 
		         {
		         IsNumber = false;
		         }
		      }
		   return IsNumber;
		   
		   }
</script>
<h2><?php echo $records['survey']->getSurveyName(); ?><hr/></h2>
<?php if (isset($_GET['message'])) {
		
		$expString  = $_GET['message'];
		
?>
		<font class="<?php //echo $col_def?>" size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<?php echo $expString; ?>
		</font>
<?php }	?>
<form id="frmSurveyProcess" name="frmSurveyProcess" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=SubmitSurvey">
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
    	<th class="tableTopMiddle" width= "600"></th>    
		<th class="tableTopRight"></th>
	</tr>
	
  </thead>
 
  <tbody>
  
<?php
	$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
	$dayleft = (strtotime($records['survey']->getSurveyToDate())-$today) / 86400;
	$logedinUser = $_SESSION['empID'];
	if(!$records['survey']->getID()){ ?>
		<tr>
	  		<td class="tableMiddleLeft"></td>
	    	<td ><span style="font-weight:bold;color:red;"><?php echo "This survey has been removed"; ?></span></td>
			<td class="tableMiddleRight"></td>
	  	</tr>	
	<?php }elseif(!in_array($logedinUser,explode(",",$records['survey']->getSurveyUsers()))){ ?>
		<tr>
	  		<td class="tableMiddleLeft"></td>
	    	<td ><span style="font-weight:bold;color:red;"><?php echo "You are not invited to do this survey"; ?></span></td>
			<td class="tableMiddleRight"></td>
	  	</tr>	
	<?php }elseif($records['survey']->getSurveyActive() ==0){ ?>
		<tr>
	  		<td class="tableMiddleLeft"></td>
	    	<td ><span style="font-weight:bold;color:red;"><?php echo "This survey has been inactive"; ?></span></td>
			<td class="tableMiddleRight"></td>
	  	</tr>
	 	
	<?php }elseif ($dayleft<0){ ?>
		<tr>
	  		<td class="tableMiddleLeft"></td>
	    	<td ><span style="font-weight:bold;color:red;"><?php echo "This survey has been expired" ?></span></td>
			<td class="tableMiddleRight"></td>
	  	</tr>
	<?php }else{
	 $answers = Answers::getAnswersBySurveyUser($records['survey']->getId(),$_SESSION['empID']);
	 if(count($answers)>0){ ?>
	 	<tr>
	  		<td class="tableMiddleLeft"></td>
	    	<td ><span style="font-weight:bold;color:red;"><?php echo "You have done this survey" ?></span></td>
			<td class="tableMiddleRight"></td>
	  	</tr>
	<?php }else{
	?>
	<tr>
  		<td class="tableMiddleLeft"></td>
    	<td ><span style="font-weight:bold;color:green;"><?php echo $records['survey']->getSurveyDescription(); ?></span></td>
		<td class="tableMiddleRight"></td>
    </tr>	
	<?php	
	 		
	$j = 0;
	$index =1;
	$questionArr=null;
	
	if (is_array($records['questions']) && count($records['questions'])>0) {
		$options =null;		
		foreach ($records['questions'] as $record) {			
			if(!($j%2)) { $cssClass = 'odd';
				} else {  	$cssClass = 'even';  }						 
				
				if(( $record->getAnswerType() == 2 || $record->getAnswerType() == 3 )){
					$options = $record->getOptions($record->getId());
					if($record->getAnswerType() == 3){
						$type = "type='checkbox'";
						$name ="[]";
					}
					else
					{
						$type = "type='radio'";
						$name ='';
					}
					?>
					
					<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>" style="font-weight:bold;"><?php echo "Question ".$index." :". $record->getText(); ?></td>
						<td class="tableMiddleRight"></td>
			  		</tr>
			  		<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>">
					    	<table>
					    		<?php foreach ($options as $option) { ?>	
					    		<tr>
					    		    		
					    			<td>
					    			<input <?php echo $type; ?> name='<?php echo $record->getId().$name; ?>' value='<?php echo $option->getId(); ?>' /><?php echo $option->getText(); ?>
					    			<td>	    		
					    		
					    		</tr>
					    		<?php } ?>
					    	</table>
				    	</td>
						<td class="tableMiddleRight"></td>
			  		</tr>	
					<?php
					$questionArr[$j]=$record->getId();
			 		$j++;
			 		$index++;
		 		}else if($record->getAnswerType() == 1){ ?>
		 			<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>" style="font-weight:bold;"><?php echo "Question ".$index." :".$record->getText(); ?></td>
						<td class="tableMiddleRight"></td>
			  		</tr>
			  		<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>">
					    	<table>
					    		<tr>
					    			<td>
					    			<input type='radio' name='<?php echo $record->getId(); ?>' value ="1" />Yes
					    			<td>
					    			<td>
					    			<input type='radio' name='<?php echo $record->getId(); ?>' value="0" />No
					    			<td>	    		
					    		</tr>
					    	</table>
				    	</td>
						<td class="tableMiddleRight"></td>
			  		</tr>	
		 		<?php
		 		$questionArr[$j]=$record->getId();
		 		$j++;
		 		$index++; 
		 		}else if($record->getAnswerType() == 4){ ?>
		 			<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>" style="font-weight:bold;"><?php echo "Question ".$index." :".$record->getText(); ?></td>
						<td class="tableMiddleRight"></td>
			  		</tr>
			  		<tr>
				  		<td class="tableMiddleLeft"></td>
				    	<td class="<?php echo $cssClass; ?>">
					    	<TEXTAREA  id="<?php echo $record->getId(); ?>" name="<?php echo $record->getId(); ?>" value="" COLS="70" ROWS="1"/></TEXTAREA>
					    	<input type="hidden" name="quizanswer" id="quizanswer" value="<?php echo $record->getId(); ?>" >
				    	</td>
						<td class="tableMiddleRight"></td>
			  		</tr>	
		 		<?php
		 		$questionArr[$j]=$record->getId();
		 		$j++; 
		 		$index++;
		 		}
			}
	}  else{ ?>
	<tr>
	  	<td class="tableMiddleLeft"></td>
	    <td class=""><?php echo $lang_Start_Survey_No_Question; ?></td>
		<td class="tableMiddleRight"></td>
	</tr>
			
		<?php }
	
	?>
  <tr>
  		<td class="tableMiddleLeft"></td>
    	<td ><span style="font-weight:bold;color:green;"><?php echo $records['survey']->getSurveyThanks(); ?></span></td>
		<td class="tableMiddleRight"></td>
  </tr>		
  </tbody>
  <tfoot>
  	<tr>
			<td class="tableMiddleLeft"></td>			
			<td >
				
				<input type="hidden" name="SurveyID" value="<?php echo $records['survey']->getId(); ?>" >
				<input type="image"  onclick="return addSave();" name="btnSave" alt="Save"
					   src="../../themes/beyondT/pictures/btn_save.gif" />
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
	<?php } 
	}
	?>
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>			
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>
</table>

